<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" label-width="120px" :rules="rules" class="form-container" :inline="true">
      <div class="createPost-main-container">
          <el-row>
            <el-col :span="12" >
              <el-form-item  label="是否通知客服:"   :required="true"  prop="postForm.status" >
                <el-radio-group v-model="postForm.status">
                  <el-radio  :label="1">是</el-radio>
                  <el-radio  :label="0">否</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>

        <el-row>
          <el-col :span="12" >
            <el-form-item  label="客服负责人:"   style="width:500px;"  :required="true"  prop="name" >
              <el-input v-model="postForm.name" style="width:300px;"   :maxlength="50"   />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" >
            <el-form-item  label="通知方式:"   :required="true" >
              <el-checkbox-group v-model="isChecked1" @change="valueChange1" size="medium">
                <el-checkbox label="邮件" ></el-checkbox>
              </el-checkbox-group>
              <el-checkbox-group v-model="isChecked2"  @change="valueChange2" size="medium">
                <el-checkbox  value="1" >短信</el-checkbox>
              </el-checkbox-group>
              <el-checkbox-group v-model="isChecked3"  @change="valueChange3" size="medium">
                <el-checkbox  value="1" >钉钉</el-checkbox>
              </el-checkbox-group>
              <el-checkbox-group v-model="isChecked4"  @change="valueChange4" size="medium">
                <el-checkbox  value="1" >微信</el-checkbox>
              </el-checkbox-group>

            </el-form-item>
          </el-col>
        </el-row>

          <el-row>
            <el-col :span="12" >
              <el-form-item  label=" 邮件:" style="width:500px;"  prop="email" >
                <el-input  v-model="postForm.email"  style="width:300px;" :maxlength="100"   />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12" >
              <el-form-item  label="钉钉:" style="width:500px;"  prop="dingding"  >
                <el-input v-model="postForm.dingding"  style="width:300px;"  :maxlength="50"   />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12" >
              <el-form-item label="微信号:" style="width:500px;">
                <el-input v-model="postForm.openid"  style="width:300px;"  :maxlength="50"   />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12" >
              <el-form-item label="短信:" style="width:500px;">
                <el-input v-model="postForm.mobile"  style="width:300px;"  :maxlength="50"   />
              </el-form-item>
            </el-col>
          </el-row>

        <el-col :span="12" style="padding-left:60px;">
          <el-button type="primary" size="small" icon="el-icon-lock" @click="submitForm">提  交</el-button>
          <el-button  type="primary" size="small" icon="el-icon-lock"  @click="resetForm">重  置</el-button>
          <el-button type="primary" size="small" icon="el-icon-lock"  @click="goback" >返 回 列 表</el-button>
        </el-col>
      </div>
    </el-form>

  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload/singleImage3'
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
import { userSearch } from '@/api/remoteSearch'
import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from '@/components/Dropdown'
import {roomAlarmList , roomAlarmDetail , updateRoomAlarm , deleteRoomAlarm } from "../../../api/roomalarm"

import { dtulist }     from "../../../api/dtu"
import { customerList }     from "../../../api/customer"

import { storageList }   from  '@/api/storage'
const defaultForm = {
  id: "",
  mobile: "",
  openid: "",
  ctype: "",
  dingding: "",
  email: "",
  name:'',
  readValue1: '',
  readValue2: '',
  readValue3: '',
  readValue4: '',
  status: 0,
  area_code:'86',
}

export default {
  name: 'OperationEdit',
  components: { Tinymce, MDinput, Upload, Sticky,   CommentDropdown, PlatformDropdown, SourceUrlDropdown },
  filters: {
    statusName(status) {
      if (status==1) {
        return '可用';
      }else{
        return '禁用'
      }
    }
  },
  props: {
    isEdit: true
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        if(rule.field == 'name'){
          callback(new Error('客服名必填！'))
        }
        // if(rule.field == 'ctype'){
        //   callback(new Error('是否通知必填！'))
        // }
      } else {
        callback()
      }
    }
    return {
      isChecked1: false,
      isChecked2: false,
      isChecked3: false,
      isChecked4: false,
      isChecked5: false,
      postForm: Object.assign({}, defaultForm),
      storageListArr:[] ,
      dtuListArr:[], //dtu列表
      typeArr: [], //类型数组
      customListArr:[],
      editForm:{
        id: '',
        admin_id: 3,
        openid: '',
        mobile: '',
        dingding: '',
        email: '',
        name: '',
        ctype: '',
        status: 0,
        area_code:'86',
        readValue1: '',
        readValue2: '',
        readValue3: '',
        readValue4: '',
        readValue5: '',
        readValue6: '',
      },
      loading: false,
      userListOptions: [],
      title:'',
      rules: {
        name:   [{ validator: validateRequire }],
      },
    }
  },
  computed: {
  },
  created() {
    console.log(this.$route.query.id)
    if (this.isEdit ) {
      const id = this.$route.query && this.$route.query.id
      this.operationDetail( id )
    } else {
      this.postForm = Object.assign({}, defaultForm)
    }
    // dtulist( {"rid":1,"page":1, "limit":20, "status":0}).then(response => {
    //   this.dtuListArr = response.data.data
    // })
    // storageList( {"uissssd":1269,"page":1, "limit":2000, "status":0} ).then(response => {
    //   this.storageListArr = response.data.data
    // })
    // customerList( {"uissssd":1269,"page":1, "limit":20, "status":0} ).then(response => {
    //   this.customListArr = response.data.data
    // })
    this.typeArr[0] = '请选择类型'
  },
  methods: {
    valueChange1(value){
      this.$nextTick(()=>{
        this.editForm.readValue1 = parseInt(value)
        console.log(value)
      })
    },
    valueChange2(value){
      this.$nextTick(()=>{
        this.editForm.readValue2 = parseInt(value)
      })
    },
    valueChange3(value){
      this.$nextTick(()=>{
        this.editForm.readValue3 = parseInt(value)
      })
    },
    valueChange4(value){
      this.$nextTick(()=>{
        this.editForm.readValue4 = parseInt(value)
      })
    },
    valueChange5(value){
      this.$nextTick(()=>{
        this.editForm.readValue5 = parseInt(value)
      })
    },
    operationDetail(id) {
      roomAlarmDetail({ rid: id}).then(response => {
        this.postForm = response.data.data
        if( this.postForm.realValue1 == 1 ){
          this.isChecked1 = true
        }
        if( this.postForm.realValue2 == 1 ){
          this.isChecked2 = true
        }
        if( this.postForm.realValue3 == 1 ){
          this.isChecked3 = true
        }
        if( this.postForm.realValue4 == 1 ){
          this.isChecked4 = true
        }
        // if( this.postForm.realValue15 == 1 ){
        //   this.isChecked5 = true
        // }
        // this.title += '  控制器 Id:${this.postForm.id}'

      }).catch(err => {
        console.log(err)
      })
    },
    resetForm() {
      this.$refs['postForm'].resetFields()
    },
    submitForm() {
      // console.log(this.$route.query.id)
      this.postForm.rid = this.$route.query.id
      this.postForm.status = 1
      console.log(this.postForm)
      this.doSubmitForm()
      // this.$refs.postForm.validate(valid => {
      //   if (valid) {
      //     this.loading = true
      //     //
      //   } else {
      //     console.log('error submit!!')
      //     return false
      //   }
      // })
    },

    doSubmitForm() {
      this.editForm.id = this.postForm.id
      this.editForm.name = this.postForm.name
      this.editForm.dingding = this.postForm.dingding
      this.editForm.admin_id = this.postForm.admin_id
      this.editForm.email = this.postForm.email
      this.editForm.mobile = this.postForm.mobile
      this.editForm.openid = this.postForm.openid
      this.editForm.ctype = this.postForm.ctype
      this.editForm.status = this.postForm.status

      // this.editForm.sid =  114 //this.postForm.sid
      this.editForm.rid =  this.postForm.rid
      if(this.isChecked1 == true){
        this.editForm.readValue1 = 1
      }
      if(this.isChecked2 == true){
        this.editForm.readValue2 = 1
      }
      if(this.isChecked3 == true){
        this.editForm.readValue3 = 1
      }
      if(this.isChecked4 == true){
        this.editForm.readValue4 = 1
      }
      // if(this.isChecked5 == true){
      //   this.editForm.readValue5 = 1
      // }

      updateRoomAlarm(this.editForm).then(response => {
        if(response.data.code == 0){
          this.$message({
            message:  '保存成功!',
            type: 'success'
          })
        }else{
          this.$message({
            message:  '保存失败!',
            type: 'error'
          })
        }
        // this.list = response.data.items
        // this.total = response.data.total
        this.listLoading = false
      })
    },
    getRemoteUserList(query) {

    },
    goback(){
      this.$router.push('/operation/list');
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/mixin.scss";
.createPost-container {
  position: relative;
  .createPost-main-container {
    padding: 40px 45px 20px 50px;
    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;
      .postInfo-container-item {
        float: left;
      }
    }
    .editor-container {
      min-height: 500px;
      margin: 0 0 30px;
      .editor-upload-btn-container {
        text-align: right;
        margin-right: 10px;
        .editor-upload-btn {
          display: inline-block;
        }
      }
    }
  }
  .word-counter {
    width: 40px;
    position: absolute;
    right: -10px;
    top: 0px;
  }
}
</style>
